package com.example.myui.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.myui.R;

import java.security.spec.PSSParameterSpec;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private static final String TAG = SearchActivity.class.getSimpleName();
    private Context mContext;
    private LinearLayout group;
    private View cover;
    private int widthPixels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mContext = this;
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        widthPixels = outMetrics.widthPixels;
        initView();
    }

    private void initView() {
        RecyclerView first = findViewById(R.id.first);
        RecyclerView second = findViewById(R.id.second);
        RecyclerView third = findViewById(R.id.third);
        cover = findViewById(R.id.cover);
        group = findViewById(R.id.view_group);
        cover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (group.getVisibility() == View.VISIBLE) {
                    hidePop();
                }
            }
        });

        group.setVisibility(View.GONE);
        group.animate().xBy(widthPixels).setDuration(10);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        first.setLayoutManager(linearLayoutManager);
        second.setLayoutManager(linearLayoutManager1);
        third.setLayoutManager(linearLayoutManager2);

        FirstAdapter firstAdapter = new FirstAdapter(getFirst());
        first.setAdapter(firstAdapter);
        firstAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i(TAG, "onItemClick: " + position);
                showPop();
                firstAdapter.setCurrentPosition(position);
            }
        });

        SecondAdapter secondAdapter = new SecondAdapter(getSecond());
        second.setAdapter(secondAdapter);
    }

    private ArrayList<String> getFirst() {
        ArrayList<String> first = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            first.add(" item1:" + i);
        }
        return first;
    }

    private ArrayList<String> getSecond() {
        ArrayList<String> first = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            first.add(" item2 :" + i);
        }
        return first;
    }

    private ArrayList<String> getThird() {
        ArrayList<String> first = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            first.add(" item3 :" + i);
        }
        return first;
    }

    private class FirstAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        private int pos = -1;

        public FirstAdapter(@Nullable List<String> data) {
            super(R.layout.item_text, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, String item) {
            holder.setText(R.id.tv_content, item);
            if (holder.getLayoutPosition() == pos) {
                holder.setTextColor(R.id.tv_content, Color.RED);
            } else {
                holder.setTextColor(R.id.tv_content, Color.BLACK);
            }
        }

        public void setCurrentPosition(int position) {
            this.pos = position;
            notifyDataSetChanged();
        }
    }

    private void showPop() {
        group.setVisibility(View.VISIBLE);
        group.animate().xBy(-widthPixels).setDuration(350);
    }

    private void hidePop() {
        if (group.getX() > 0) return;
        group.animate().xBy(widthPixels).setDuration(350);
    }

    private class SecondAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public SecondAdapter(@Nullable List<String> data) {
            super(R.layout.item_text, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, String item) {
            holder.setText(R.id.tv_content, item);
        }
    }
}
